package com.niuwa.p2p.action.customer;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.customer.ICustomerService;
import com.niuwa.p2p.service.filesystem.FileSystem;
import com.niuwa.p2p.vo.customer.CustomerVo;

public class AccountOperAction extends JsonAction {

	/**
	 *
	 */
	private static final long serialVersionUID = 2018652743259076351L;

	/** 用户Servcie spring扫包注入 */
	@Autowired
	private ICustomerService customerService;
	
	// 文件系统
	@Autowired
	private FileSystem fileSystem;
	
	/** Grid条件查找参数 */
	private String queryParamKey;

	/** Grid条件查找参数值*/
	private String queryParamValue;
	
	/** 客户Id*/
	private long custId;
	
	/** 账户禁用申请理由*/
	private String writeOffReasonCust;
	
	/** 账户禁用申请的上传文件ID*/
	private String uploadedFileIdCust;
	
	/** 资金账户禁用申请理由*/
	private String writeOffReasonAccount;
	
	/** 资金账户禁用申请的上传文件ID*/
	private String uploadedFileIdAccount;
	
	public String getWriteOffReasonCust() {
		return writeOffReasonCust;
	}

	public void setWriteOffReasonCust(String writeOffReasonCust) {
		this.writeOffReasonCust = writeOffReasonCust;
	}


	public String getUploadedFileIdCust() {
		return uploadedFileIdCust;
	}

	public void setUploadedFileIdCust(String uploadedFileIdCust) {
		this.uploadedFileIdCust = uploadedFileIdCust;
	}

	public String getWriteOffReasonAccount() {
		return writeOffReasonAccount;
	}

	public void setWriteOffReasonAccount(String writeOffReasonAccount) {
		this.writeOffReasonAccount = writeOffReasonAccount;
	}

	public String getUploadedFileIdAccount() {
		return uploadedFileIdAccount;
	}

	public void setUploadedFileIdAccount(String uploadedFileIdAccount) {
		this.uploadedFileIdAccount = uploadedFileIdAccount;
	}

	public long getCustId() {
		return custId;
	}

	public void setCustId(long custId) {
		this.custId = custId;
	}

	public String getQueryParamKey() {
		return queryParamKey;
	}

	public void setQueryParamKey(String queryParamKey) {
		this.queryParamKey = queryParamKey;
	}

	public String getQueryParamValue() {
		return queryParamValue;
	}

	public void setQueryParamValue(String queryParamValue) {
		this.queryParamValue = queryParamValue;
	}

	/**
	 * 跳转到账户操作视图
	 * 
	 * @return 账户操作视图
	 */	
	@Action("gotoAccountOper")
	public String gotoAccountOper() {
		return "accountOper";
	}

	/**
	 * 锁定用户 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(锁定失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("frozeCustomer")
	public String frozeCustomer() throws Exception {
		JsonResult result = new JsonResult();		
		customerService.updateCustomerToFroze(custId, SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 解锁用户 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(解锁失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("unfrozeCustomer")
	public String unfrozeCustomer() throws Exception {
		JsonResult result = new JsonResult();		
		customerService.updateCustomerToUnfroze(custId, SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 禁用用户 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(禁用失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("disableCustomer")
	public String disableCustomer() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateCustomerToDisable(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 启用用户 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(启用失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("enableCustomer")
	public String enableCustomer() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateCustomerToEnable(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 注销用户 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(注销失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("writeOffCustomer")
	public String writeOffCustomer() throws Exception {
		JsonResult result = new JsonResult();
		// 检查注销原因非空
		if(StringUtil.isEmpty(writeOffReasonCust)) {
			result.setSuccess(false);
			result.setMsg("注销原因不能为空");
			this.setResult(result);
			return NONE;
		} 

		// 检查注销上传文件存在性
		if(fileSystem.getFile(uploadedFileIdCust) == null) {
			result.setSuccess(false);
			result.setMsg("上传的资料找不到");
			this.setResult(result);
			return NONE;
		}		
		customerService.updateCustomerToWriteOff(custId, writeOffReasonCust, uploadedFileIdCust, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 锁定账户
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(锁定失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("frozeAccout")
	public String frozeAccout() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateToAccountForze(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 解锁账户
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(解锁失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("unfrozeAccout")
	public String unfrozeAccout() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateAccountToUnfroze(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 禁用账户
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(禁用失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("disableAccout")
	public String disableAccout() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateAccountToDisable(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 启用账户
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(启用失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("enableAccout")
	public String enableAccout() throws Exception {
		JsonResult result = new JsonResult();
		customerService.updateAccountToEnable(custId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	/**
	 * 注销账户
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(注销失败,客户不存在)
	 * @throws Exception 
	 */
	@Action("writeOffAccout")
	public String writeOffAccout() throws Exception {
		JsonResult result = new JsonResult();
		// 检查注销原因非空
		if(StringUtil.isEmpty(writeOffReasonAccount)) {
			result.setSuccess(false);
			result.setMsg("注销原因不能为空");
			this.setResult(result);
			return NONE;
		}

		// 检查注销上传文件存在性
		if(fileSystem.getFile(uploadedFileIdAccount) == null) {
			result.setSuccess(false);
			result.setMsg("上传的资料找不到");
			this.setResult(result);
			return NONE;
		}
		customerService.updateAccountToWriteOff(custId, writeOffReasonAccount, uploadedFileIdAccount, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	@Action("listCustomer")
	public String listCustomer() {

		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		// 参数并非与数据库key，Value对应，做相应转换
		// 例： queryParamKey：loginName queryParamValue：jsj =>存入Map为loginName：jsj 
		if(paramMap.get("queryParamKey") != null && paramMap.get("queryParamKey") instanceof String){
			queryParamKey = (String) paramMap.get("queryParamKey");
			queryParamValue = (String) paramMap.get("queryParamValue");
			paramMap.put((String) paramMap.get("queryParamKey"), paramMap.get("queryParamValue"));
		}

		// 执行查询操作
		PageBean<CustomerVo> pagination = customerService.search(paramMap);
		// 设置返回
		setObjectReSult(pagination);

		return NONE;
	}

}
